<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <code-tech.diandian.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 扩展后台管理页面
 * @author yangweijie <yangweijiester@gmail.com>
 */
class ActMoreBuyController extends AdminController {

  public  function index(){
      $actGoods=M('activity_more_buy')->join("gr_product ON gr_activity_more_buy.id = gr_product.id")->select();
      $list=M('product')->where(array("status"=>1))->select();
//      var_dump($actGoods);
      $this->assign('actGoods',$actGoods);
      $this->assign('goodsList',$list);
      $this->display();
  }
  public  function insert(){
      $arr=I();
      if(count($arr)>0){
          $i =count($arr['price1']);
          $arr1=Array();
          $action = 0;
          $flag = false;
          $result=0;
          for($j=0;$j<$i;$j++){
              if($arr['old'][$j]=="1"||$arr['old'][$j]==1){
                  $map['id']=$arr['pid'][$j];
                  if($arr['price1'][$j] == ""|| $arr['price1'][$j] == 0||$arr['num1'][$j] == ""||$arr['num1'][$j] == 0){
                      $action = "delete";
                  }else{
                      $data1['price1']=$arr['price1'][$j];
                      $data1['num1']=$arr['num1'][$j];
                      $action = "update";
                      if($arr['price2'][$j] == ""|| $arr['price2'][$j] == 0||$arr['num2'][$j] == ""||$arr['num2'][$j] == 0){
                          $data1['price2']=0;
                          $data1['num2']=0;
                      }else{
                          $data1['price2']=$arr['price2'][$j];
                          $data1['num2']=$arr['num2'][$j];
                      }
                  }
                  switch($action){
                      case "delete":
                          $result+=D('activity_more_buy')->where($map)->delete();
                          break;
                      case "update":
                          $result+=D('activity_more_buy')->where($map)->save($data1);
                          break;

                  }
                  continue;
              }
              if($arr['price1'][$j]==""||$arr['num1'][$j]==""){
                  $flag=true;
                  continue;
              }else{
                  $data['id']=$arr['pid'][$j];
                  $data['pname']=$arr['pname'][$j];
                  $data['price1']=$arr['price1'][$j];
                  $data['num1']=$arr['num1'][$j];
                  if($arr['price2'][$j] != ""||$arr['num2'][$j] != ""){
                      $data['price2']=$arr['price2'][$j];
                      $data['num2']=$arr['num2'][$j];
                  }
              }
              array_push($arr1,$data);
          }
         $res= D('activity_more_buy')->addAll($arr1);
          if($result !== false||$res){
              if($flag){
                  $this->success("修改成功,但部分商品优惠价格或数量未设置!", U('ActMoreBuy/index'),1);
              }else{
                  $this->success("修改成功!", U('ActMoreBuy/index'),1);
              }
          }else{
              if($flag){
                  $this->error("修改失败,部分商品优惠价格或数量未设置!", U('ActMoreBuy/index'),1);
              }else{
                  $this->error("修改失败!", U('ActMoreBuy/index'),1);
              }

          }
      }else{
          $this->success("没有新数据！", U('ActMoreBuy/index'),1);
      }


  }
    public function deleteById($id){
        $data['id']=$id;
        $res=D('activity_more_buy')->where($data)->delete();
        if($res){
            $this->success("删除成功!");
        }else{
            $this->error("删除失败！");
        }
    }



}
